home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / gravity < prev    next >
Internet Message Format  |  1995-03-31  |  14KB

  1. From comp.sys.handhelds Sun Jun  9 13:59:55 1991
  2. Path: seq!ecsgate!mcnc!gatech!psuvax1!wuarchive!sdd.hp.com!hp-col!hpldola!hp-lsd!dfk
  3. From: dfk@hp-lsd.COS.HP.COM (David F. Kurth)
  4. Newsgroups: comp.sys.handhelds
  5. Subject: Gravity Game for HP48SX
  6. Message-ID: <10580003@hp-lsd.COS.HP.COM>
  7. Date: 7 Jun 91 16:15:15 GMT
  8. Organization: HP Logic Systems Division - Col
  9. Lines: 374
  10.  
  11. This is my first attempt at writing anything substantial for the
  12. HP48 calculator.  I wrote this in PASCAL about 10 years ago for
  13. a HP3000 computer, then rewrote it in BASIC for a primative home
  14. computer, and then massaged it into this for a great handheld calculator.
  15. I am not a programmer, but enjoy trying to get something to work
  16. the way I want.  This program is by no means perfect, but I enjoy
  17. playing it and wanted to share with everyone else.
  18. Have fun playing.
  19.  
  20. Dave Kurth
  21. dfk@hp-lsd.cos.hp.com
  22.  
  23. %%HP: T(3)A(R)F(.);
  24. @ GRAVITY GAME
  25. @
  26. @ Original Concept Stolen From Some Ancient HP Desktop Calculator
  27. @
  28. @ Written for HP48 by Dave Kurth  06Jun91  Ver 1.0
  29. @
  30. @ Object:  Launch a missile from your station (a cross or X
  31. @ on the left side of the screen) to hit a target (a bull's-eye
  32. @ on the right side of the screen).  Two to five planets affect
  33. @ the missile's path due to their gravitational field.  Larger 
  34. @ diameter planets have more influence.
  35. @
  36. @ Controls: Your only input is the firing angle of your missile.
  37. @ An angle of 0 degrees shoots horizontal to the right.  90 degrees
  38. @ is straight up.  If there is too much planet mass behind your
  39. @ station, you may not be able to reach the target.  On the other
  40. @ hand, an indirect missile path may have surprising results.
  41. @ Be careful, or you may hit yourself!  Your station and the target
  42. @ have no gravity, so they will not "attract" the missile.
  43. @
  44. @ Caution: This game is realtime.  One must realize that when using
  45. @ convention missiles without warp drive travelling way below the 
  46. @ speed of light, it takes time to cover such astronomical distances.
  47. @ This game is ideal for long boring meetings where you have time 
  48. @ to burn and want everyone to think you're still solving those 
  49. @ complicated problems that *required* the purchase of your calculator 
  50. @ in the first place.
  51. @
  52. @ Starting: Download the program to the calculator.  This is in
  53. @ straight ASCII format, so there is no need to mess with ASC->
  54. @ binary, uuencode, or any other format.  Then push the GRAV menu key 
  55. @ to start the program.
  56. @
  57. @ For a simple demo, enter "2" for a seed and 10 degrees for the angle.
  58. @ This direct shot takes about 30 seconds.
  59. @ For a crazy demo, enter "2" for a seed and 20 degrees for the angle.
  60. @ Now wait about 7 minutes and see what a different path the missile
  61. @ follows to hit the target.  When the missile is travelling, any
  62. @ keystroke (other than ATTN/ON) will abort the current shot.  You 
  63. @ may abort because you think your shot will miss.  If you let the 
  64. @ missile continue though, you may find the missile will go around 
  65. @ again and eventually hit the target.  This is what makes the game
  66. @ fun to watch and predict.
  67. @
  68. @ This is presented with no rights reserved.  I take no responsibility
  69. @ for any damage or injury to anyone caused by this program.  I
  70. @ believe the physics are correct, but then again all the constants
  71. @ are made up to fit the "universe" on the screen.
  72. @ I'm no expert on the HP48 (I've only had one since Feb).  I only
  73. @ wish things would go faster.  If you find a way to speed things up, 
  74. @ please post the info so I and everyone else may be enlightened.  
  75. @ Above all, have fun and maybe you'll learn something new.
  76. @
  77. @ Directory
  78. @ Checksum # CDEEh
  79. @ Bytes      4654
  80. @ Checksum will change after program is run due to creation of variables
  81. @
  82. @
  83. DIR
  84.   GRAV
  85.     \<< RCLF \-> f           @ Save status
  86.       \<< 2 CF INIT 2 SF     @ Set up variables
  87.         WHILE 2 FS?          @ Start main loop
  88.         REPEAT
  89.           SENDMIS
  90.         END
  91.         f STOF               @ Restore status
  92.       \>>
  93.       { GRAV } ORDER 2 MENU  @ Restore VAR menu with GRAV first
  94.       KILL                   @ Remove HALT annuciator.
  95.     \>>
  96.   INIT
  97.     \<< GETSEED ERASE { #0d #0d} PVIEW
  98.       5 'MAX.PL' STO       @ Max Number of Planets
  99.       4 'T.SIZE' STO       @ Target Size radius
  100.       3 'S.SIZE' STO       @ Station Size radius
  101.       10 'MAX.MIS' STO     @ Max Number of Missles
  102.       10 'MNUM' STO        @ Missle Number of Current Shot
  103.       5 'TIME.MAG' STO     @ Time Magnification when off-screen
  104.       1E-3 'MIN.FORC' STO  @ Minumum Force to determine Missile lost
  105.       12 'VINIT' STO       @ Initial Missle Velocity
  106.       360 'Gk' STO         @ Gravitational Konstant
  107.       0 'ANGLE' STO        @ Initial Missle Velocity Angle
  108.       0.2 'DTIME' STO      @ Delta Time
  109.       { } 'POSITION' STO   @ Position/Mass Array, Station, Target, + Planets
  110.       MAX.PL 1 - RAND * IP 2 + 'NUM.P' STO  @ Random number of Planets (2-5)
  111.       GENSTAT
  112.       GENTARG
  113.       GENPLAN
  114.       ANGLE SETMIS @ Set missile data in case user doesn't first time
  115.     \>>
  116.   GETSEED  @ Set random number seed
  117.     \<< 
  118.       "Enter random seed or\010"
  119.       "Nothing if don't care\010" +
  120.       "Push ENTER" +
  121.       ""
  122.       INPUT DUP SIZE
  123.       IF 0 \=/
  124.       THEN
  125.         OBJ\-> RDZ @ If number entered, set seed
  126.       ELSE
  127.         DROP       @ Else clear stack
  128.       END
  129.     \>>
  130.   GENTARG  @ Locate and draw target in right half of screen
  131.     \<< 
  132.       POSITION
  133.       24 RAND * IP 87 +   @ X value
  134.       24 RAND * IP 20 +   @ Y value
  135.       T.SIZE              @ Mass (radius) value
  136.       3 \->LIST 1 \->LIST + 'POSITION' STO
  137.       DTARG
  138.     \>>
  139.   DTARG  @ Draw target
  140.     \<<
  141.       PICT POSITION 2 GET OBJ\-> DROP2 SWAP 4 - R\->B
  142.       SWAP 4 - R\->B 2 \->LIST TARG REPL
  143.     \>>
  144.   GENSTAT  @ Locate and draw station in left half of screen
  145.     \<<
  146.       POSITION
  147.       24 RAND * IP 20 +   @ X value
  148.       24 RAND * IP 20 +   @ Y value
  149.       S.SIZE              @ Mass (radius) value
  150.       3 \->LIST 1 \->LIST + 'POSITION' STO
  151.       DSTAT
  152.     \>>
  153.   DSTAT  @ Draw station
  154.     \<< 
  155.       PICT POSITION 1 GET OBJ\-> DROP2 SWAP 3 - R\->B
  156.       SWAP 3 - R\->B 2 \->LIST STAT REPL
  157.     \>>
  158.   GENPLAN  @ Locate and draw planets with separation
  159.     \<< 3 0 0 \-> i x y
  160.       \<<
  161.         DO
  162.           POSITION
  163.           110 RAND * IP 10 + DUP R\->B 'x' STO  @ X value
  164.           40 RAND * IP 10 + DUP R\->B 'y' STO   @ Y value
  165.           x y 2 \->LIST DUP 'x' STO PIXON       @ Show location temporarily
  166.           5 RAND * IP 2 +                       @ Mass value
  167.           3 \->LIST 1 \->LIST + 'POSITION' STO
  168.           1 CF
  169.           1 i 1 -
  170.           FOR j
  171.             i j CALDIFF
  172.             IF 12 <
  173.             THEN 1 SF
  174.             END
  175.           NEXT
  176.           x PIXOFF
  177.           IF 1 FC?
  178.           THEN      @ Location is valid, draw planet
  179.             i DPLAN 1 'i' STO+
  180.           ELSE      @ Invalid location, remove from list
  181.             POSITION DUP SIZE 1 - 1 SWAP SUB 'POSITION' STO
  182.           END
  183.         UNTIL i NUM.P 2 + >
  184.         END
  185.       \>>
  186.     \>>
  187.   DPLAN  @ Draw one planet
  188.     \<< 
  189.       POSITION SWAP GET OBJ\-> DROP R\->B 3 ROLLD
  190.       R\->B SWAP R\->B SWAP 2 \->LIST SWAP
  191.       0 6.28 RAD ARC
  192.     \>>
  193.   CALDIFF  @ Calculate distance between surface of planets i & j
  194.     \<< \-> i j
  195.       \<<  @ DIFF = SQRT((Yj-Yi)^2 + (Xj-Xi)^2) - Mj - Mi
  196.         POSITION j GET OBJ\-> DROP POSITION i GET OBJ\-> DROP
  197.         4 ROLL + 5 ROLLD SWAP 3 ROLLD - DUP * 3 ROLLD - DUP *
  198.         + \v/ SWAP -
  199.       \>>
  200.     \>>
  201.   REDRAW  @ Draw station, target, and planets again
  202.     \<< 
  203.       ERASE { #0d #0d } PVIEW DSTAT DTARG
  204.       3 NUM.P 2 +
  205.       FOR j
  206.         j DPLAN
  207.       NEXT
  208.     \>>
  209.   SENDMIS  @ Send a missile
  210.     \<<
  211.       3 CF            @ Outside Station Flag (clear for inside)
  212.       0 'HitSom' STO  @ 0 = hit nothing
  213.                       @ 1 = hit planet
  214.                       @ 2 = hit target
  215.                       @ 3 = hit station
  216.                       @ 4 = lost in space
  217.                       @ 5 = user aborted missile
  218.                       @ 6 = quit program
  219.       IF MNUM 0 ==
  220.       THEN 2 CF       @ Out missles, quit
  221.       ELSE
  222.         TEXT CLLCD    @ Activate and clear text screen
  223.         {
  224.           { "VIEW" \<< { #0d #0d} PVIEW WAITK DHELP \>> }
  225.           { "REDR" \<< REDRAW DHELP \>> }
  226.           { "SHOT" \<< TEXT CLLCD ""
  227.                      IF MNUM MAX.MIS \=/
  228.                      THEN
  229.                        "Last Angle was " +
  230.                        2 FIX ANGLE \->STR + "\010" +
  231.                      END
  232.                      "Input angle, push CONT" +
  233.                      "    90"   3 DISP
  234.                      "180 ** 0" 4 DISP
  235.                      "   -90"   5 DISP
  236.                      3 FREEZE
  237.                      PROMPT
  238.                      DUP 'ANGLE' STO SETMIS DHELP \>> }
  239.           { "CONT" CONT }
  240.           { "GO" CONT }
  241.           { "QUIT" \<< 2 CF 6 'HitSom' STO CONT \>> }
  242.         }
  243.         TMENU DHELP                  @ Display menu and help text
  244.         HALT
  245.         MNUM 1 - 'MNUM' STO          @ One less missile now
  246.         { #0d #0d } PVIEW
  247.         WHILE HitSom NOT             @ Until we hit something
  248.         REPEAT UPDATE DRAWXY EVALPOS @ Calculate missle movement
  249.         END
  250.       END
  251.     \>>
  252.   SETMIS  @ Set initial values for missile data, angle is on stack
  253.     \<<
  254.       NEG DUP DEG COS VINIT * 'VX' STO SIN 
  255.       VINIT * 'VY' STO POSITION 1 GET OBJ\-> DROP2 'YM' STO 
  256.       'XM' STO 
  257.     \>> 
  258.   DHELP  @ Display help for temporary menu
  259.     \<<
  260.       TEXT CLLCD 
  261.       "VIEW - See Planets"     1 DISP
  262.       "REDR - Redraw Screen"   2 DISP
  263.       "SHOT - Enter Angle"     3 DISP
  264.       "GO   - Launch Missile"  4 DISP
  265.       "QUIT - Restore and End" 5 DISP
  266.       MNUM STD \->STR " Missle" + 
  267.       IF MNUM 1 \=/ THEN "s" + END " Left" + 7 DISP
  268.       3 FREEZE
  269.     \>>
  270.   UPDATE  @ Update missile position and velocity
  271.     \<< 0 0 \-> fx fy
  272.       \<<
  273.         IF XM DUP 0 < SWAP 130 > OR
  274.            YM DUP 0 < SWAP  63 > OR OR  @ If off screen
  275.         THEN TIME.MAG DTIME *           @ increase time interval
  276.         ELSE DTIME 
  277.         END 'T' STO
  278.         3 NUM.P 2 +
  279.         FOR i                           @ For each planet
  280.           i CALDIST \-> x y m d         @ Cal dist to missle
  281.           \<<
  282.             y YM - m * Gk * d DUP DUP * * DUP 3 ROLLD /
  283.             'fy' STO+
  284.             x XM - m * Gk * SWAP /
  285.             'fx' STO+                   @ Sum resulting forces
  286.           \>>
  287.         NEXT
  288.         fx T * 'VX' STO+ fy T * 'VY' STO+  @ From total force
  289.         VX T * 'XM' STO+ VY T * 'YM' STO+  @ Find new velocity
  290.         fx DUP * fy DUP * + 'FORCE' STO    @ and missile location
  291.       \>>
  292.     \>>
  293.   CALDIST  @ Calculate distance from missile to center of planet
  294.     \<<
  295.       POSITION SWAP GET DUP OBJ\-> DROP2 YM - DUP * SWAP XM - DUP * + \v/
  296.       SWAP OBJ\-> DROP 4 ROLL   @ Return X, Y, Mass, Distance
  297.     \>>
  298.   DRAWXY  @ Draw pixel on screen for missile position
  299.     \<<
  300.       XM R\->B YM R\->B 2 \->LIST PIXON
  301.     \>>
  302.   EVALPOS  @ Evaluate missile position
  303.     \<<
  304.       IF KEY                      @ If a key is hit, abort missile
  305.       THEN
  306.         5 'HitSom' STO
  307.       ELSE
  308.         IF DTIME TIME.MAG * T ==  @ If off screen
  309.         THEN 
  310.           IF FORCE MIN.FORC <   @ Check if lost in space
  311.           THEN 4 'HitSom' STO 
  312.           END
  313.         ELSE                    @ Else when on screen
  314.           3 NUM.P 2 +           @ Check if we hit a planet
  315.           FOR i
  316.             i CALDIST \-> x y m d 
  317.             \<<
  318.               IF d m \<= THEN 1 'HitSom' STO END
  319.             \>>
  320.           NEXT
  321.           1 CALDIST             @ Check if we hit the station
  322.           IF 3 FC?              @ IF not outside station
  323.           THEN
  324.             IF S.SIZE 1 + >     @ Are we outside yet?
  325.             THEN 3 SF           @ We are now outside the station
  326.             END
  327.           ELSE                  @ We are outside
  328.             IF S.SIZE 1 + \<=   @ did we hit ourself?
  329.             THEN 3 'HitSom' STO
  330.             END
  331.           END
  332.           2 CALDIST             @ Check if we hit the target
  333.           IF T.SIZE 1 + \<= THEN 2 'HitSom' STO END
  334.         END
  335.       END
  336.       CLEAR                     @ Clear CALDIST stuff off stack
  337.       MESS                      @ Output appropriate message
  338.     \>>
  339.   MESS  @ Display appropriate message
  340.     \<<
  341.       IF HitSom
  342.       THEN
  343.         TEXT CLLCD              @ Display text screen and clear
  344.         CASE
  345.           HitSom 1 ==
  346.           THEN "Hit Planet"
  347.           END
  348.           HitSom 2 ==
  349.           THEN "You Hit the Target\010Using "
  350.             MAX.MIS MNUM - STD \->STR +
  351.             " Missile" +
  352.             IF MAX.MIS MNUM - 1 \=/
  353.             THEN "s" +
  354.             END
  355.             2 CF              @ Stop program
  356.           END
  357.           HitSom 3 ==
  358.           THEN 
  359.             "You Blew Yourself Up" 
  360.             2 CF              @ Stop program
  361.           END
  362.           HitSom 4 ==
  363.           THEN "Missle Escaped System"
  364.           END
  365.           HitSom 5 ==
  366.           THEN "Missle Aborted"
  367.           END
  368.         END
  369.         1 DISP 
  370.         IF MNUM 0 == 
  371.         THEN
  372.           "Game Over No Missiles" 3 DISP
  373.         END
  374.         "  Push any key EXCEPT"   4 DISP
  375.         "  ATTN/ON to continue"   5 DISP WAITK
  376.       END
  377.     \>>
  378.   WAITK  @ Wait for any key press
  379.     \<<
  380.       0 WAIT DROP
  381.     \>>
  382.   STAT GROB 7 7 C141F755F741C1                        @ Station cross
  383.   TARG GROB 9 9 8300EF006C00BB10BA10BB106C00EF008300  @ Target bullseye
  384. END
  385.  
  386.